package org.cargloss.cp.controller.modul.menus;

import org.cargloss.cp.model.Menus;

import util.hibernate.DAO;

import com.opensymphony.xwork2.ActionSupport;

public class ActionCrud extends ActionSupport {

	private static final long serialVersionUID = -5933849517817526086L;

	private int id;

	private String name;
	private String toolTip;
	private String parent;
	private String url;
	private String extUrl;

	public String getExtUrl() {
		return extUrl;
	}

	public void setExtUrl(String extUrl) {
		this.extUrl = extUrl;
	}

	public String insert() {
		DAO<Integer> dao = new DAO<Integer>();
		Menus object = new Menus();
		object.setName(getName());
		object.setToolTip(getToolTip());

		if (!getUrl().equals("Ext"))
			object.setUrl(getUrl());
		else
			object.setUrl(getExtUrl());
		if (!getParent().equals("0")) {
			Menus m = new Menus();
			m.setId(Integer.valueOf(getParent()));
			object.setParent(m);
		} else {
			object.setParent(null);
		}

		dao.insert(object);
		dao.close();
		return "empty";
	}

	public String update() {

		DAO<Integer> dao = new DAO<Integer>();
		Menus object = (Menus) dao.find(Menus.class, Integer.valueOf(getId()));

		object.setName(getName());
		object.setToolTip(getToolTip());

		if (!getUrl().equals("Ext"))
			object.setUrl(getUrl());
		else
			object.setUrl(getExtUrl());

		if (!getParent().equals("0")) {
			Menus m = new Menus();
			m.setId(Integer.valueOf(getParent()));
			object.setParent(m);
		} else {
			object.setParent(null);
		}

		dao.update(object);
		dao.close();
		return "empty";
	}

	public String delete() {
		DAO<Integer> dao = new DAO<Integer>();
		Menus delete = (Menus) dao.find(Menus.class, Integer.valueOf(getId()));
		dao.delete(delete);
		dao.close();
		return "empty";
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getToolTip() {
		return toolTip;
	}

	public void setToolTip(String toolTip) {
		this.toolTip = toolTip;
	}

	public String getParent() {
		return parent;
	}

	public void setParent(String parent) {
		this.parent = parent;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

}
